package com.xlh.controller;

import com.xlh.aop.PreAcquireLock;
import com.xlh.common.Result;
import com.xlh.dto.course.ZtreeDTO;
import com.xlh.security.SpringSecurityUserInfoHolder;
import com.xlh.service.ConvertToolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.xlh.constant.ConvertToolConstant.CONVERT_MESSAGE;
import static com.xlh.constant.ConvertToolConstant.CONVERT_TOOL;

/**
 * 课程数据转换工具
 * <p>
 * Created by lx on 2019/11/26.
 */
@Api(tags = "ConvertToolController", description = "转换工具")
@RestController
@RequestMapping("/convert-tool")
public class ConvertToolController {

    @Autowired
    private ConvertToolService convertToolService;

    @ApiOperation("获取远程课程+章节 以及 当前课程+章节 对比")
    @PreAuthorize("permitAll()")
    @GetMapping("/ztree")
    public Result<List<ZtreeDTO>> queryRemoteCourseChapterZtree(
            @RequestParam @ApiParam(value = "远程服务器数据库ip", required = true) String remoteDBIp,
            @RequestParam @ApiParam(value = "远程服务器数据库端口号", required = true) String remoteDBPort,
            @RequestParam @ApiParam(value = "远程服务器数据库库名", required = true) String remoteDBName,
            @RequestParam @ApiParam(value = "远程服务器数据库用户", required = true) String remoteDBUser,
            @RequestParam @ApiParam(value = "远程服务器数据库密码", required = true) String remoteDBPassword) {
        return Result.success(convertToolService.queryRemoteCourseChapterZtree(
                remoteDBIp, remoteDBPort, remoteDBName, remoteDBUser, remoteDBPassword));
    }

    @ApiOperation("将远程服务器章节进行导入")
    @PreAuthorize("permitAll()")
    @PostMapping("/import")
    @PreAcquireLock(value = CONVERT_TOOL, message = CONVERT_MESSAGE)
    public Result importRemoteCourseChapter(
            @RequestParam @ApiParam(value = "远程服务器数据库ip", required = true) String remoteDBIp,
            @RequestParam @ApiParam(value = "远程服务器数据库端口号", required = true) String remoteDBPort,
            @RequestParam @ApiParam(value = "远程服务器数据库库名", required = true) String remoteDBName,
            @RequestParam @ApiParam(value = "远程服务器数据库用户", required = true) String remoteDBUser,
            @RequestParam @ApiParam(value = "远程服务器数据库密码", required = true) String remoteDBPassword,

            @RequestParam @ApiParam(value = "远程服务器ip", required = true) String remoteIp,
            @RequestParam @ApiParam(value = "远程服务器用户名", required = true) String remoteUser,
            @RequestParam(required = false) @ApiParam(value = "远程服务器密码") String remotePassword,
            @RequestParam @ApiParam(value = "远程服务器静态资源路径", required = true) String remoteResourcesPath,
            @RequestParam @ApiParam(value = "远程服务器ssh端口", required = true) Integer remotePort,

            @RequestParam(required = false, defaultValue = "false")
            @ApiParam(value = "导入课程前是否清库 true：是 false：不清库") Boolean isCleanDB,

            @RequestBody @ApiParam(value = "远程服务器需要导入的章节数据", required = true) List<Long> chapterIds,
            @Autowired SpringSecurityUserInfoHolder userInfoHolder) {

        // 清库、清资源
        if (isCleanDB) {
            convertToolService.cleanDB();
        }

        // 导课
        convertToolService.importCourseAndSendMessage(
                remoteDBIp, remoteDBPort, remoteDBName, remoteDBUser,
                remoteDBPassword, remoteIp, remoteUser, remotePassword, remotePort, remoteResourcesPath, chapterIds,
                userInfoHolder.getUserId());

        return Result.success();

    }

}
